package com.lepu.app.fun.chat.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.Const;
import com.app.utils.HeadIconLoadUtil;
import com.app.utils.LoginUtil;
import com.app.utils.SdLocal;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.HttpRequest;
import com.core.lib.utils.CallBack;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.utils.secure.EncodeMD5;
import com.core.lib.widget.CircularImageView;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.chat.bean.BeanLectureChatUser;
import com.lepu.app.fun.chat.bean.ChatRoomMessage;
import com.lepu.app.fun.chat.bean.Media;
import com.lepu.app.fun.chat.util.ChatConst;
import com.lepu.app.fun.chat.util.ChatRoomCallBack;
import com.lepu.app.fun.chat.util.ChatSendListener;
import com.lepu.app.fun.chat.util.Constants;
import com.lepu.app.fun.chat.util.DateUtil;
import com.lepu.app.fun.chat.util.LectureUtil;
import com.lepu.app.fun.chat.util.ManagerClipborad;
import com.lepu.app.fun.chat.util.VoicePlayClickListenerRoomMessage;
import com.lepu.app.main.activity.AppImageBroswerActivity;
import com.lepu.pasm.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * 讲座聊天列表
 */
public class ChatRoomMessageAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private Context mContext = null;
    private List<ChatRoomMessage> mArrayList = null;
    private int mType = ChatConst.TYPE_ONLINE_CHAT_ONLINE;
    private ChatSendListener mChatSendListener;

    private LinkedList<String> mImageDataContainer = new LinkedList<String>();
    private String mGroupID = "";
    ListView mListView;

    ChatRoomCallBack mChatRoomCallBack;

    public ChatRoomMessageAdapter(Context context,
                                  ChatSendListener chatSendListener, List<ChatRoomMessage> arrayList,
                                  int type,String groupID,ListView listView,ChatRoomCallBack chatRoomCallBack) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
        mType = type;
        mChatSendListener = chatSendListener;
        mGroupID = groupID;
        mListView = listView;
        mChatRoomCallBack = chatRoomCallBack;

        // 初始化图片数组
        if (mArrayList != null && mArrayList.size() > 0) {

            for (int i = 0;i<mArrayList.size();i++) {
                addImageData(mArrayList.get(i),true);
            }
        }
    }

    @Override
    public int getCount() {
        int size = 0;
        if (mArrayList != null && mArrayList.size() > 0) {
            size = mArrayList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public void addImageData(ChatRoomMessage message,boolean isLast)
    {
        try {
            if (message.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_IMAGE) {

                String url = "";
                if (message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE)
                {
                    url = message.getMedia().getLargeImageId();
                }else{
                    url = message.getMedia().getFullName();
                }

                if(TextUtils.isEmpty(url))
                {
                    url = message.getMedia().getFullName();
                }

                if(isLast)
                {
                    mImageDataContainer.addLast(url);
                }else{
                    mImageDataContainer.addFirst(url);
                }
            }
        }catch (Exception e){

        }
    }

    public void removeImageData(ChatRoomMessage message)
    {
        try {
            if (message.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_IMAGE) {

                String url = "";
                if (message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE)
                {
                    url = message.getMedia().getLargeImageId();
                }else{
                    url = message.getMedia().getFullName();
                }

                mImageDataContainer.remove(url);
            }
        }catch (Exception e){
        }
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(
                    R.layout.chat_message_listitem, null);

            holder = new ViewHolder();

            TextView timeTextView = (TextView) convertView
                    .findViewById(R.id.timeTextView);
            // receive
            RelativeLayout receiveMessageLayout = (RelativeLayout) convertView
                    .findViewById(R.id.receiveMessageLayout);
            CircularImageView receiveHeadIconImageView = (CircularImageView) convertView
                    .findViewById(R.id.receiveHeadIconImageView);
            TextView receiveNameTextView = (TextView) convertView
                    .findViewById(R.id.receiveNameTextView);// ?????
            LinearLayout receiveContentLayout = (LinearLayout) convertView
                    .findViewById(R.id.receiveContentLayout);
            TextView receiveContentTextView = (TextView) convertView
                    .findViewById(R.id.receiveContentTextView);
            ImageView receiveContentImageView = (ImageView) convertView
                    .findViewById(R.id.receiveContentImageView);
            LinearLayout receiveContentYuYinLayout = (LinearLayout) convertView
                    .findViewById(R.id.receiveContentYuYinLayout);

            TextView receiveContentYuYinTimeTextView = (TextView) convertView
                    .findViewById(R.id.receiveContentYuYinTimeTextView);
            ImageView receiveChatVoiceImageView = (ImageView) convertView
                    .findViewById(R.id.receiveChatVoiceImageView);

            LinearLayout receiveYuYinExtraLayout = (LinearLayout) convertView
                    .findViewById(R.id.receiveYuYinExtraLayout);
            ImageView receiveYuyinReadImageView = (ImageView) convertView
                    .findViewById(R.id.receiveYuyinReadImageView);
            ProgressBar receiveContentProgressBar = (ProgressBar) convertView
                    .findViewById(R.id.receiveContentProgressBar);
            ImageView receiveContentTipImageView = (ImageView) convertView
                    .findViewById(R.id.receiveContentTipImageView);

            // send
            RelativeLayout sendMessageLayout = (RelativeLayout) convertView
                    .findViewById(R.id.sendMessageLayout);
            CircularImageView sendHeadIconImageView = (CircularImageView) convertView
                    .findViewById(R.id.sendHeadIconImageView);
            TextView sendNameTextView = (TextView) convertView
                    .findViewById(R.id.sendNameTextView);// ?????
            LinearLayout sendContentLayout = (LinearLayout) convertView
                    .findViewById(R.id.sendContentLayout);
            TextView sendContentTextView = (TextView) convertView
                    .findViewById(R.id.sendContentTextView);
            ImageView sendContentImageView = (ImageView) convertView
                    .findViewById(R.id.sendContentImageView);
            LinearLayout sendContentYuYinLayout = (LinearLayout) convertView
                    .findViewById(R.id.sendContentYuYinLayout);
            TextView sendContentYuYinTimeTextView = (TextView) convertView
                    .findViewById(R.id.sendContentYuYinTimeTextView);
            ImageView sendChatVoiceImageView = (ImageView) convertView
                    .findViewById(R.id.sendChatVoiceImageView);
            //
            ProgressBar sendContentProgressBar = (ProgressBar) convertView
                    .findViewById(R.id.sendContentProgressBar);
            ImageView sendContentReImageView = (ImageView) convertView
                    .findViewById(R.id.sendContentReImageView);

            LinearLayout sendYuYinExtraLayout = (LinearLayout) convertView
                    .findViewById(R.id.sendYuYinExtraLayout);

            ImageView sendContentTipImageView = (ImageView) convertView
                    .findViewById(R.id.sendContentTipImageView);
            LinearLayout sendDetailInfoLayout = (LinearLayout) convertView
                    .findViewById(R.id.sendDetailInfoLayout);
            TextView sendDetailInfoTipTextView = (TextView) convertView
                    .findViewById(R.id.sendDetailInfoTipTextView);

            holder.timeTextView = timeTextView;

            holder.receiveMessageLayout = receiveMessageLayout;
            holder.receiveHeadIconImageView = receiveHeadIconImageView;
            holder.receiveNameTextView = receiveNameTextView;
            holder.receiveContentLayout = receiveContentLayout;
            holder.receiveContentTextView = receiveContentTextView;
            holder.receiveContentImageView = receiveContentImageView;
            holder.receiveContentYuYinLayout = receiveContentYuYinLayout;
            holder.receiveContentYuYinTimeTextView = receiveContentYuYinTimeTextView;
            holder.receiveYuYinExtraLayout = receiveYuYinExtraLayout;
            holder.receiveYuyinReadImageView = receiveYuyinReadImageView;
            holder.receiveChatVoiceImageView = receiveChatVoiceImageView;
            holder.receiveContentProgressBar = receiveContentProgressBar;
            holder.receiveContentTipImageView = receiveContentTipImageView;

            holder.sendMessageLayout = sendMessageLayout;
            holder.sendHeadIconImageView = sendHeadIconImageView;
            holder.sendNameTextView = sendNameTextView;
            holder.sendContentLayout = sendContentLayout;
            holder.sendContentTextView = sendContentTextView;
            holder.sendContentImageView = sendContentImageView;
            holder.sendContentYuYinLayout = sendContentYuYinLayout;
            holder.sendContentYuYinTimeTextView = sendContentYuYinTimeTextView;

            holder.sendContentProgressBar = sendContentProgressBar;
            holder.sendContentReImageView = sendContentReImageView;
            holder.sendYuYinExtraLayout = sendYuYinExtraLayout;
            holder.sendChatVoiceImageView = sendChatVoiceImageView;

            holder.sendContentTipImageView = sendContentTipImageView;
            holder.sendDetailInfoLayout = sendDetailInfoLayout;
            holder.sendDetailInfoTipTextView = sendDetailInfoTipTextView;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            resetViewHolder(holder);
        }

        final ChatRoomMessage item = mArrayList.get(position);

        // time show
        if (position > 0) {
            if (DateUtil.isCloseEnough(item.getSendTime(),
                    mArrayList.get(position - 1).getSendTime())) {
                holder.timeTextView.setVisibility(View.GONE);
            } else {
                holder.timeTextView.setText(DateUtil.formatChatDateTime(item
                        .getSendTime()));
                holder.timeTextView.setVisibility(View.VISIBLE);
            }
        } else {
            holder.timeTextView.setVisibility(View.VISIBLE);
            holder.timeTextView.setText(DateUtil.formatChatDateTime(item
                    .getSendTime()));
        }

        // receive
        if (item.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE) {

            // receive
            holder.receiveMessageLayout.setVisibility(View.VISIBLE);
            holder.sendMessageLayout.setVisibility(View.GONE);

            if (mType == ChatConst.TYPE_ONLINE_CHAT_LECTURE) {
                holder.receiveNameTextView.setVisibility(View.VISIBLE);
                holder.receiveContentProgressBar.setVisibility(View.GONE);

                String toUserID = String.valueOf(item.getToUserId());
                BeanLectureChatUser beanLectureChatUser = LectureUtil.getInstance().getGroupChatUser(toUserID);
                if (beanLectureChatUser != null) {
                    holder.receiveNameTextView.setVisibility(View.VISIBLE);
                    holder.receiveNameTextView.setText(beanLectureChatUser.NickName);
                    HeadIconLoadUtil.displayImage(R.drawable.app_ic_default_avatar, Setting.getImageUrl(beanLectureChatUser.Avatar),
                            holder.receiveHeadIconImageView);

                    holder.receiveHeadIconImageView.setOnLongClickListener(new HeadIconOnLongClickListener(beanLectureChatUser));
                } else {
                    //// TODO: 2016/7/12
                    LogUtilBase.LogD(null, "ChatMessageAdapter no Avatar");

                    holder.receiveHeadIconImageView
                            .setImageResource(R.drawable.app_ic_default_avatar);
                    holder.receiveNameTextView.setVisibility(View.VISIBLE);
                    holder.receiveNameTextView.setText("");

                    LectureUtil.getInstance().requestSimpleGroupInfo((BaseFragmentActivity) mContext, toUserID, mGroupID, new CallBack() {
                        @Override
                        public void callBackSuccess(Object obj) {
                            super.callBackSuccess(obj);

                            BeanLectureChatUser item = (BeanLectureChatUser) obj;
                            if(item != null){
                                holder.receiveNameTextView.setVisibility(View.VISIBLE);
                                holder.receiveNameTextView.setText(item.NickName);
                                HeadIconLoadUtil.displayImage(R.drawable.app_ic_default_avatar, Setting.getImageUrl(item.Avatar),
                                        holder.receiveHeadIconImageView);

                                holder.receiveHeadIconImageView.setOnLongClickListener(new HeadIconOnLongClickListener(item));
                            }
                        }

                        @Override
                        public void callBackFail(Object obj) {
                            super.callBackFail(obj);
                        }
                    });
                }

                holder.receiveHeadIconImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoginUtil.jumpUserInfo((BaseFragmentActivity) mContext, String.valueOf(item.getToUserId()));
                    }
                });
            } else {
                holder.receiveNameTextView.setVisibility(View.GONE);

                HeadIconLoadUtil.displayImage(R.drawable.chat_doctor_icon_default, Setting.getImageUrl(item.getGuestProfilePicId()),
                        holder.receiveHeadIconImageView);
            }

            holder.receiveContentLayout.setTag(0);
            holder.receiveContentTipImageView.setVisibility(View.GONE);

            holder.receiveContentLayout.setOnClickListener(null);
            holder.receiveContentLayout.setOnLongClickListener(null);

            int messageType = item.getMessageType();
            final Media media = item.getMedia();
            switch (messageType) {
                case 1:// text
                    holder.receiveContentTextView.setVisibility(View.VISIBLE);
                    holder.receiveContentImageView.setVisibility(View.GONE);
                    holder.receiveContentYuYinLayout.setVisibility(View.GONE);
                    holder.receiveYuYinExtraLayout.setVisibility(View.GONE);

                    holder.receiveContentTextView.setText(item.getContent());

                    holder.receiveContentLayout
                            .setOnLongClickListener(new OnLongClickListener() {

                                @Override
                                public boolean onLongClick(View v) {
                                    String content = holder.receiveContentTextView
                                            .getText().toString();
                                    contentTextClick(content);
                                    return false;
                                }
                            });

                    break;
                case 2:// image
                    holder.receiveContentTextView.setVisibility(View.GONE);
                    holder.receiveContentImageView.setVisibility(View.VISIBLE);
                    holder.receiveContentYuYinLayout.setVisibility(View.GONE);
                    holder.receiveYuYinExtraLayout.setVisibility(View.GONE);

//                    ViewGroup.LayoutParams paramReceiveImage = holder.receiveContentImageView.getLayoutParams();
//                    handleImageWidth(paramReceiveImage, Const.CHAT_IMAGE_WIDTH_RECEIVE,0);

//                    String filePath = "";
//                    if (media != null && media.getFullName() != null) {
//                        filePath = media.getFullName();
//                    }
//                    UniversalImageLoadTool.disPlay(
//                            ImageUtilBase.getFileImageUrl(filePath),
//                            holder.receiveContentImageView,R.drawable.default_icon_small,
//                            R.drawable.app_chat_image_download_error);

                    String imageUrl = media.getLargeImageId();
                    UniversalImageLoadTool.disPlay(imageUrl,
                            holder.receiveContentImageView,R.drawable.default_icon_small,
                            R.drawable.app_chat_image_download_error);

                    String fileImagePath = ImageLoader.getInstance().getDiskCache().get(imageUrl).getPath();
                    if(!TextUtils.isEmpty(fileImagePath) && new File(fileImagePath).exists()){
                        int[] widthHeightArray = ImageUtilBase.getImageWidthHeight(fileImagePath);
                        int srcWidth = widthHeightArray[0];
                        int srcHeight = widthHeightArray[1];

                        int newHeight = Const.CHAT_IMAGE_WIDTH_SEND * srcHeight / srcWidth;
                        ViewGroup.LayoutParams paramReceiveImage = holder.receiveContentImageView.getLayoutParams();
                        handleImageWidth(paramReceiveImage, Const.CHAT_IMAGE_WIDTH_SEND, newHeight);
                    }

                    holder.receiveContentLayout
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    contentImageClick(item);
                                }
                            });

                    break;
                case 3:// voice
                    holder.receiveContentTextView.setVisibility(View.GONE);
                    holder.receiveContentImageView.setVisibility(View.GONE);
                    holder.receiveContentYuYinLayout.setVisibility(View.VISIBLE);
                    holder.receiveYuYinExtraLayout.setVisibility(View.VISIBLE);

                    int duration = item.getMedia().getDuration();
                    String time = String.valueOf(duration / 1000 == 0 ? 1
                            : duration / 1000) + "''";
                    holder.receiveContentYuYinTimeTextView.setText(time);
                    holder.receiveContentYuYinTimeTextView.setVisibility(View.VISIBLE);

                    LinearLayout.LayoutParams paramReceive = (LinearLayout.LayoutParams) holder.receiveContentYuYinLayout
                            .getLayoutParams();
                    handleVoiceTextWidth(paramReceive, time);

                    if (!item.isRead()) {
                        holder.receiveYuyinReadImageView
                                .setVisibility(View.VISIBLE);
                    } else {
                        holder.receiveYuyinReadImageView.setVisibility(View.GONE);
                    }
                    holder.receiveChatVoiceImageView.setImageResource(R.drawable.chatfrom_voice_playing);

                    holder.receiveContentLayout.setTag(item.getMessageId());
                    holder.receiveContentLayout
                            .setOnClickListener(new VoicePlayClickListenerRoomMessage(item,
                                    holder.receiveChatVoiceImageView,
                                    holder.receiveYuyinReadImageView, mContext, position, mListView));

                    String downloadUrl = media.getObjectId();
                    String fileName = EncodeMD5.getMd5(downloadUrl);
                    String filePath = SdLocal.getChatVoicePath(MyApplication.getInstance(), LoginUtil.getUserId(), fileName);

                    holder.receiveContentProgressBar.setTag(downloadUrl);
                    holder.receiveContentTipImageView.setTag(String.format("%s_tip",downloadUrl));

                    File file = new File(filePath);
                    if(!file.exists())
                    {
                        holder.receiveContentProgressBar.setVisibility(View.VISIBLE);
                        //download audio file
                        downloadFile(downloadUrl);
                    }else{
                        holder.receiveContentTipImageView.setVisibility(View.GONE);
                    }

                    break;
                case 4:// video
                    break;
            }
        } else {
            // send
            final String userId = LoginUtil.getUserId();

            BeanLectureChatUser beanLectureChatUser = new BeanLectureChatUser();
            beanLectureChatUser.UserID = userId;
            beanLectureChatUser.NickName = MyApplication.getInstance().getLoginInfo().NickName;
            holder.sendHeadIconImageView.setOnLongClickListener(new HeadIconOnLongClickListener(beanLectureChatUser));

            holder.receiveMessageLayout.setVisibility(View.GONE);
            holder.sendMessageLayout.setVisibility(View.VISIBLE);

            holder.sendNameTextView.setVisibility(View.GONE);

//            holder.sendHeadIconImageView
//                    .setImageResource(R.drawable.app_ic_default_avatar);

            LoginUtil.loadSelfUserImage(mContext, holder.sendHeadIconImageView, userId, R.drawable.app_ic_default_avatar);

            holder.sendContentLayout.setOnClickListener(null);
            holder.sendContentLayout.setOnLongClickListener(null);

            if (mType == ChatConst.TYPE_ONLINE_CHAT_LECTURE) {
                holder.sendHeadIconImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoginUtil.jumpUserInfo((BaseFragmentActivity) mContext, userId);
                    }
                });
            }

            int messageType = item.getMessageType();
            final Media media = item.getMedia();
            switch (messageType) {
                case 1:// text
                    holder.sendContentTextView.setVisibility(View.VISIBLE);
                    holder.sendContentImageView.setVisibility(View.GONE);
                    holder.sendContentYuYinLayout.setVisibility(View.GONE);
                    holder.sendYuYinExtraLayout.setVisibility(View.GONE);

                    holder.sendContentTextView.setText(item.getContent());

                    holder.sendContentLayout
                            .setOnLongClickListener(new OnLongClickListener() {

                                @Override
                                public boolean onLongClick(View v) {
                                    String content = holder.sendContentTextView
                                            .getText().toString();
                                    contentTextClick(content);
                                    return false;
                                }
                            });

                    break;
                case 2:// image
                    holder.sendContentTextView.setVisibility(View.GONE);
                    holder.sendContentImageView.setVisibility(View.VISIBLE);
                    holder.sendContentYuYinLayout.setVisibility(View.GONE);
                    holder.sendYuYinExtraLayout.setVisibility(View.GONE);

                    String filePath = "";
                    if (media != null && media.getFullName() != null) {
                        filePath = media.getFullName();
                    }

                    int[] widthHeightArray = ImageUtilBase.getImageWidthHeight(filePath);
                    int srcWidth = widthHeightArray[0];
                    int srcHeight = widthHeightArray[1];

                    int newHeight = Const.CHAT_IMAGE_WIDTH_SEND * srcHeight / srcWidth;
                    ViewGroup.LayoutParams paramSendImage = holder.sendContentImageView.getLayoutParams();
                    handleImageWidth(paramSendImage,Const.CHAT_IMAGE_WIDTH_SEND,newHeight);

                    UniversalImageLoadTool.disPlay(ImageUtilBase.getFileImageUrl(filePath),
                            holder.sendContentImageView,R.drawable.default_icon_small,
                            R.drawable.app_chat_image_download_error);

                    holder.sendContentLayout
                            .setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    contentImageClick(item);
                                }
                            });

                    break;
                case 3:// voice
                    holder.sendContentTextView.setVisibility(View.GONE);
                    holder.sendContentImageView.setVisibility(View.GONE);
                    holder.sendContentYuYinLayout.setVisibility(View.VISIBLE);
                    holder.sendYuYinExtraLayout.setVisibility(View.VISIBLE);

                    int duration = item.getMedia().getDuration();
                    String time = String.valueOf(duration / 1000 == 0 ? 1
                            : duration / 1000) + "''";
                    holder.sendContentYuYinTimeTextView.setText(time);

                    LinearLayout.LayoutParams paramSend = (LinearLayout.LayoutParams) holder.sendContentYuYinLayout
                            .getLayoutParams();
                    handleVoiceTextWidth(paramSend, time);

                    holder.sendChatVoiceImageView.setImageResource(R.drawable.chatto_voice_playing);

                    holder.sendContentLayout
                            .setOnClickListener(new VoicePlayClickListenerRoomMessage(item,
                                    holder.sendChatVoiceImageView, null, mContext,position,mListView));
                    break;
                case 4:// video

                    break;
            }

            holder.sendContentReImageView
                    .setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            reSendContentCheck(position, item);
                        }
                    });

            int sendStatus = item.getSendStatus();
            if (sendStatus == Constants.Chat.CHAT_SEND_STATUS_SENT) {
                holder.sendContentProgressBar.setVisibility(View.VISIBLE);
                holder.sendContentReImageView.setVisibility(View.GONE);
            } else if (sendStatus == Constants.Chat.CHAT_SEND_STATUS_LOST
                    || sendStatus == Constants.Chat.CHAT_SEND_STATUS_UPLOAD_FAILED) {
                holder.sendContentProgressBar.setVisibility(View.GONE);
                holder.sendContentReImageView.setVisibility(View.VISIBLE);
            } else {
                holder.sendContentProgressBar.setVisibility(View.GONE);
                holder.sendContentReImageView.setVisibility(View.GONE);
            }

            if (sendStatus == Constants.Chat.CHAT_SEND_STATUS_USER_FORBIDEN) {
                //被禁言
                holder.sendContentTipImageView.setVisibility(View.VISIBLE);
                holder.sendDetailInfoLayout.setVisibility(View.VISIBLE);
                holder.sendDetailInfoTipTextView.setText(R.string.chat_lecture_block_tip);
            } else {
                holder.sendContentTipImageView.setVisibility(View.GONE);
                holder.sendDetailInfoLayout.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    private void downloadFile(final String ServerUrl) {
        final String fileName = EncodeMD5.getMd5(ServerUrl);
        final String ServerPath = SdLocal.getChatVoicePath(mContext, LoginUtil.getUserId(), fileName);

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpRequest.downloadFileBy(ServerUrl, ServerPath, new CallBack() {
                    @Override
                    public void callBackSuccess(Object obj) {
                        super.callBackSuccess(obj);

                        Message msg = mHandler.obtainMessage();
                        msg.what = 10;
                        msg.obj = ServerUrl;
                        mHandler.sendMessage(msg);
                    }

                    @Override
                    public void callBackFail(Object obj) {
                        super.callBackFail(obj);
                        Message msg = mHandler.obtainMessage();
                        msg.what = 11;
                        msg.obj = ServerUrl;
                        mHandler.sendMessage(msg);
                    }
                });
            }
        }).start();
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 10:
                case 11:

                    String url = (String) msg.obj;

                    ProgressBar progressBar = (ProgressBar) mListView.findViewWithTag(String.format("%s", url));
                    if (progressBar != null) {
                        progressBar.setVisibility(View.GONE);
                    }

                    if(msg.what == 11){
                        ImageView receiveContentTipImageView = (ImageView) mListView.findViewWithTag(String.format("%s_tip", url));
                        if (receiveContentTipImageView != null) {
                            receiveContentTipImageView.setVisibility(View.VISIBLE);
                        }
                    }

                    break;

                default:
                    break;
            }
        }
    };

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    private void contentTextClick(String content) {
        ManagerClipborad.getInstance().getClipboardM(mContext).setText(content);
        UIHelper.showToast(mContext, "拷贝成功");
    }

    /**
     * 图片点击
     *
     * @param message
     */
    private void contentImageClick(ChatRoomMessage message) {
        String[] urlArray = new String[mImageDataContainer.size()];
        int index = 0;

        String mediaUrl = "";
        if(message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE){
            mediaUrl = message.getMedia().getLargeImageId();
        }else{
            mediaUrl = message.getMedia().getFullName();
        }

        for (int i=0;i<mImageDataContainer.size();i++) {
            String value = mImageDataContainer.get(i);
            if (value.equals(mediaUrl)) {
                index = i;
            }
            urlArray[i] = value;
        }

        Intent intent = new Intent(mContext, AppImageBroswerActivity.class);
        intent.putExtra("isShowTopbar", false);
        intent.putExtra("index", index);
        intent.putExtra("urlArray", urlArray);
        ((BaseFragmentActivity) mContext).startActivity(intent);
    }

    private void handleImageWidth(ViewGroup.LayoutParams params,int width,int height)
    {
        params.width = width;
        if(height > 0){
            params.height = height;
        }
    }

    private void handleVoiceTextWidth(LinearLayout.LayoutParams params,
                                      String time) {
        int duration = Integer.valueOf(time.split("''")[0]);
        int maxWidth = 350;
        if ((time.split("''")[0]).contains("'")) {
            params.width = maxWidth;
            return;
        }
        if (duration < 3) {
            params.width = maxWidth-270;
        } else if (duration == 3) {
            params.width = maxWidth-250;
        } else if (duration == 4) {
            params.width = maxWidth-230;
        } else if (duration == 5) {
            params.width = maxWidth-220;
        } else if (duration == 6) {
            params.width = maxWidth-200;
        } else if (duration == 7) {
            params.width = maxWidth-180;
        } else if (duration == 8) {
            params.width = maxWidth-160;
        } else if (duration == 9) {
            params.width = maxWidth-140;
        } else if (duration > 9 && duration < 20) {
            params.width = maxWidth-120;
        } else if (duration > 19 && duration < 30) {
            params.width = maxWidth-100;
        } else if (duration > 29 && duration < 40) {
            params.width = maxWidth-80;
        } else if (duration > 39 && duration < 50) {
            params.width = maxWidth-60;
        } else if (duration > 49 && duration < 60) {
            params.width = maxWidth-40;
        } else if (duration >= 60) {
            params.width = maxWidth;
        }else{
            params.width = maxWidth;
        }
    }

    private void reSendContentCheck(final int position, ChatRoomMessage ChatRoomMessage) {
        String message = "您确定要重发这条消息吗？";
        Activity currentActivity = (Activity) mContext;
        MyDialog dialog = new MyDialog(currentActivity)
                .setMessage(message)
                .setNegativeButton(R.string.app_cancel,
                        new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {

                            }
                        })
                .setPositiveButton(R.string.app_ok, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        reSendContent(position);
                    }
                });

        dialog.create(null);
        dialog.showMyDialog();
    }

    private void reSendContent(int position) {
        if (mChatSendListener != null) {
            mChatSendListener.reSendContent(position);
        }
    }
    
    private void resetViewHolder(ViewHolder holder)
    {
        holder.timeTextView.setText("");

        // receive
        holder.receiveHeadIconImageView.setImageResource(0);
        holder.receiveNameTextView.setText("");// ?????
        holder.receiveContentTextView.setText("");
        holder.receiveContentYuYinTimeTextView.setText("");

        holder.receiveContentLayout.setTag("");

        holder.receiveContentImageView.setImageResource(0);
        holder.receiveChatVoiceImageView.setImageResource(0);

        // send
        holder.sendHeadIconImageView.setImageResource(0);
        holder.sendNameTextView.setText("");// ?????
        holder.sendContentTextView.setText("");
        holder.sendContentYuYinTimeTextView.setText("");
        holder.sendDetailInfoTipTextView.setText("");

        holder.sendContentImageView.setImageResource(0);
        holder.sendChatVoiceImageView.setImageResource(0);
    }

    class HeadIconOnLongClickListener implements OnLongClickListener{

        private BeanLectureChatUser beanLectureChatUser;

        private HeadIconOnLongClickListener(BeanLectureChatUser item)
        {
            beanLectureChatUser = item;
        }

        @Override
        public boolean onLongClick(View v) {

            mChatRoomCallBack.editTextUpdate(beanLectureChatUser);
            return true;
        }
    }

    private static class ViewHolder {
        TextView timeTextView;

        // receive
        RelativeLayout receiveMessageLayout;
        CircularImageView receiveHeadIconImageView;
        TextView receiveNameTextView;// ?????
        LinearLayout receiveContentLayout;
        TextView receiveContentTextView;
        ImageView receiveContentImageView;
        LinearLayout receiveContentYuYinLayout;

        LinearLayout receiveYuYinExtraLayout;
        ImageView receiveChatVoiceImageView;
        ImageView receiveYuyinReadImageView;
        TextView receiveContentYuYinTimeTextView;

        ProgressBar receiveContentProgressBar;
        ImageView receiveContentTipImageView;

        // send
        RelativeLayout sendMessageLayout;
        CircularImageView sendHeadIconImageView;
        TextView sendNameTextView;// ?????
        LinearLayout sendContentLayout;
        TextView sendContentTextView;
        ImageView sendContentImageView;
        LinearLayout sendContentYuYinLayout;

        ProgressBar sendContentProgressBar;
        ImageView sendContentReImageView;

        LinearLayout sendYuYinExtraLayout;
        ImageView sendChatVoiceImageView;
        TextView sendContentYuYinTimeTextView;

        ImageView sendContentTipImageView;
        LinearLayout sendDetailInfoLayout;
        TextView sendDetailInfoTipTextView;
    }
}
